package com.clouddisk.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.clouddisk.common.core.domain.entity.StorageBean;
import com.clouddisk.common.core.domain.entity.SysDept;
import com.clouddisk.common.core.domain.entity.SysUser;
import com.clouddisk.system.mapper.StorageMapper;
import com.clouddisk.system.mapper.SysDeptMapper;
import com.clouddisk.system.mapper.SysUserMapper;
import com.clouddisk.system.mapper.UserFileMapper;
import com.clouddisk.system.service.IStorageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Slf4j
@RequiredArgsConstructor
@Service
public class StorageServiceImpl extends ServiceImpl<StorageMapper, StorageBean> implements IStorageService {
    @Resource
    StorageMapper storageMapper;
    @Resource
    SysUserMapper sysUserMapper;
    @Resource
    UserFileMapper userFileMapper;

    @Resource
    SysDeptMapper sysDeptMapper;

    public Long getTotalStorageSize(Long userId, String fileDeptName) {
        LambdaQueryWrapper<StorageBean> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if ("person".equals(fileDeptName)){
            lambdaQueryWrapper.eq(StorageBean::getUserId, userId);
        }else{
            lambdaQueryWrapper.eq(StorageBean::getDeptName, fileDeptName);
        }
        StorageBean storageBean = storageMapper.selectOne(lambdaQueryWrapper);
        Long totalStorageSize;
        if (storageBean == null || storageBean.getTotalStorageSize() == null) {
            if ("person".equals(fileDeptName)){
                LambdaQueryWrapper<SysUser> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                lambdaQueryWrapper1.eq(SysUser::getUserId,userId);
                SysUser sysUser = sysUserMapper.selectOne(lambdaQueryWrapper1);
                totalStorageSize = sysUser.getQuota();
                storageBean = new StorageBean();
                storageBean.setUserId(userId);
                storageBean.setTotalStorageSize(totalStorageSize * 1024);
                storageMapper.insert(storageBean);
            }else {
                LambdaQueryWrapper<SysDept> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                lambdaQueryWrapper1.eq(SysDept::getDeptName,fileDeptName);
                SysDept sysDept = sysDeptMapper.selectOne(lambdaQueryWrapper1);
                totalStorageSize = sysDept.getQuota();
                storageBean = new StorageBean();
                storageBean.setDeptName(fileDeptName);
                storageBean.setTotalStorageSize(totalStorageSize * 1024);
                storageMapper.insert(storageBean);
            }
        } else {
            totalStorageSize = storageBean.getTotalStorageSize();
        }

        if (totalStorageSize != null) {
            totalStorageSize = totalStorageSize * 1024 * 1024;
        }
        return totalStorageSize;
    }

    public boolean checkStorage(Long userId, Long fileSize, String fileDeptName) {
        LambdaQueryWrapper<StorageBean> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if ("person".equals(fileDeptName)){
            lambdaQueryWrapper.eq(StorageBean::getUserId, userId);
        }else{
            lambdaQueryWrapper.eq(StorageBean::getDeptName, fileDeptName);
        }
        StorageBean storageBean = storageMapper.selectOne(lambdaQueryWrapper);
        Long totalStorageSize = null;
        if (storageBean == null || storageBean.getTotalStorageSize() == null) {
            if ("person".equals(fileDeptName)){
                LambdaQueryWrapper<SysUser> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                lambdaQueryWrapper1.eq(SysUser::getUserId,userId);
                SysUser sysUser = sysUserMapper.selectOne(lambdaQueryWrapper1);
                totalStorageSize = sysUser.getQuota();
                storageBean = new StorageBean();
                storageBean.setUserId(userId);
                storageBean.setTotalStorageSize(totalStorageSize * 1024);
                storageMapper.insert(storageBean);
            }else {
                LambdaQueryWrapper<SysDept> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
                lambdaQueryWrapper1.eq(SysDept::getDeptName,fileDeptName);
                SysDept sysDept = sysDeptMapper.selectOne(lambdaQueryWrapper1);
                totalStorageSize = sysDept.getQuota();
                storageBean = new StorageBean();
                storageBean.setDeptName(fileDeptName);
                storageBean.setTotalStorageSize(totalStorageSize * 1024);
                storageMapper.insert(storageBean);
            }
        } else  {
            totalStorageSize = storageBean.getTotalStorageSize();
        }

        if (totalStorageSize != null) {
            totalStorageSize = totalStorageSize * 1024 * 1024;
        }
        Long storageSize = userFileMapper.selectStorageSizeByUserId(userId,fileDeptName);
        if (storageSize == null ){
            storageSize = 0L;
        }
        return storageSize + fileSize <= totalStorageSize;
    }
}
